package com.example.budget3.util;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.provider.BaseColumns;

public class TransactionDataContract {
    // To prevent someone from accidentally instantiating the contract class,
    // give it an empty constructor.
    public TransactionDataContract() {}

    /* Inner class that defines the table contents */
    public static abstract class DataEntry implements BaseColumns {
        public static final String TABLE_NAME = "dataEntry";
        public static final String COLUMN_NAME_ENTRY_ID = "entryid";
        public static final String COLUMN_NAME_NAME = "name";
        public static final String COLUMN_NAME_COST = "cost";
        public static final String COLUMN_NAME_CATEGORY = "category";
        public static final String COLUMN_NAME_TIME = "time";
        public static final String COLUMN_NAME_TYPE = "type";
		public static final String COLUMN_NAME_NULLABLE ="NULL";
    }
    
    public static abstract class BillEntry implements BaseColumns {
        public static final String TABLE_NAME = "billEntry";
        public static final String COLUMN_NAME_ENTRY_ID = "entryid";
        public static final String COLUMN_NAME_NAME = "name";
        public static final String COLUMN_NAME_COST = "cost";
        public static final String COLUMN_NAME_CATEGORY = "category";
        public static final String COLUMN_NAME_TYPE = "type";
		public static final String COLUMN_NAME_PERIOD = "period";
		public static final String COLUMN_NAME_HOUR = "hour";
		public static final String COLUMN_NAME_MINUTE = "minute";
		public static final String COLUMN_NAME_DAY_OF_MONTH = "dayOfMonth";
		public static final String COLUMN_NAME_DAY_OF_WEEK = "dayOfWeek";
		public static final String COLUMN_NAME_NULLABLE ="NULL";
		public static final String COLUMN_NAME_NEXT_TIME = "nextTime";
    }
    
    private static final String TEXT_TYPE = " TEXT";
    private static final String INTEGER_TYPE = " INTEGER";
    private static final String REAL_TYPE = " REAL";
    private static final String BOOLEAN_TYPE = " BOOLEAN";
    private static final String DATETIME_TYPE = " DATETIME";
    private static final String COMMA_SEP = ",";
    private static final String SQL_CREATE_DATA_ENTRIES = "CREATE TABLE " 
    						+ DataEntry.TABLE_NAME + " (" + DataEntry._ID 
    						+ " INTEGER PRIMARY KEY," 
    						+ DataEntry.COLUMN_NAME_ENTRY_ID 
    						+ INTEGER_TYPE + COMMA_SEP 
    						+ DataEntry.COLUMN_NAME_NAME 
    						+ TEXT_TYPE + COMMA_SEP  
    						+ DataEntry.COLUMN_NAME_COST 
    						+ REAL_TYPE + COMMA_SEP  
    						+ DataEntry.COLUMN_NAME_CATEGORY 
    						+ INTEGER_TYPE + COMMA_SEP  
    						+ DataEntry.COLUMN_NAME_TIME 
    						+ DATETIME_TYPE + COMMA_SEP  
    						+ DataEntry.COLUMN_NAME_TYPE 
    						+ BOOLEAN_TYPE + " )";
    
    private static final String SQL_CREATE_BILL_ENTRIES = "CREATE TABLE " 
							+ BillEntry.TABLE_NAME + " (" 
							+ BillEntry._ID + " INTEGER PRIMARY KEY," 
							+ BillEntry.COLUMN_NAME_ENTRY_ID + INTEGER_TYPE + COMMA_SEP 
							+ BillEntry.COLUMN_NAME_NAME + TEXT_TYPE + COMMA_SEP  
							+ BillEntry.COLUMN_NAME_COST + REAL_TYPE + COMMA_SEP  
							+ BillEntry.COLUMN_NAME_CATEGORY + INTEGER_TYPE + COMMA_SEP  
							+ BillEntry.COLUMN_NAME_TYPE + BOOLEAN_TYPE + COMMA_SEP 
							+ BillEntry.COLUMN_NAME_PERIOD + INTEGER_TYPE + COMMA_SEP 
							+ BillEntry.COLUMN_NAME_HOUR + INTEGER_TYPE + COMMA_SEP 
							+ BillEntry.COLUMN_NAME_MINUTE + INTEGER_TYPE + COMMA_SEP 
							+ BillEntry.COLUMN_NAME_DAY_OF_MONTH + INTEGER_TYPE + COMMA_SEP 
							+ BillEntry.COLUMN_NAME_DAY_OF_WEEK + INTEGER_TYPE + COMMA_SEP
							+ BillEntry.COLUMN_NAME_NEXT_TIME + DATETIME_TYPE   
							+ " )";

    private static final String SQL_DELETE_DATA_ENTRIES =
        "DROP TABLE IF EXISTS " + DataEntry.TABLE_NAME;
    
    private static final String SQL_DELETE_BILL_ENTRIES =
            "DROP TABLE IF EXISTS " + DataEntry.TABLE_NAME;
    
    
    public class TransactionDataDbHelper extends SQLiteOpenHelper {
        // If you change the database schema, you must increment the database version.
        public static final int DATABASE_VERSION = 1;
        public static final String DATABASE_NAME = "Budget.db";

        public TransactionDataDbHelper(Context context) {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
        }
        public void onCreate(SQLiteDatabase db) {
            db.execSQL(SQL_CREATE_DATA_ENTRIES);
            db.execSQL(SQL_CREATE_BILL_ENTRIES);
        }
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            // This database is only a cache for online data, so its upgrade policy is
            // to simply to discard the data and start over
            db.execSQL(SQL_DELETE_DATA_ENTRIES);
            db.execSQL(SQL_DELETE_BILL_ENTRIES);
            onCreate(db);
        }
        public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            onUpgrade(db, oldVersion, newVersion);
        }
    }
    
}